<template>
  <div style="height: 530px; width: 100%; position: relative">
    <div class="pagetop">
      <table class="table1style">
        <tr>
          <td>
            <p style="font-weight: bold">条码号：</p>
          </td>
          <td>
            <el-input v-model="barcodetxt" style="width: 500px" @keyup.enter.native="SingRcvBarcode(barcodetxt)"
              @click.native="HightlightPos(barcodetxt)" id="barcodeid" placeholder="请扫描条码……"></el-input>
          </td>
          <td>
            <el-button @click="btnClose" icon="el-icon-close">退出</el-button>
          </td>
        </tr>
      </table>
    </div>
    <div class="pagecenter">
      <div class="middle-center">
        <vxe-table show-overflow ref="xTable1" height="auto" :select-config="{ reserve: true }" :data.sync="tableData1"
          border size="mini" show-header-overflow highlight-current-row highlight-hover-row resizable stripe>
          <vxe-table-column field="barcode" title="条码号 " width="110" header-align="center"></vxe-table-column>
          <vxe-table-column field="req_groupna" title="分单类别 " width="86" header-align="center">
            <template v-slot="{ row }">
              <div
                v-bind:style="{ background: $pub.ColorChange(row.tube_color), color: $pub.colorConvert(row.tube_color) }"
                style="height: 20px; width: 120px">{{ !!row.req_groupna ? row.req_groupna : "" }}</div>
            </template>
          </vxe-table-column>
          <vxe-table-column field="pat_no" title="病人号 " width="105" header-align="center"></vxe-table-column>
          <vxe-table-column field="pat_name" title="姓名 " width="70" header-align="center"></vxe-table-column>
          <vxe-table-column field="pat_sexname" title="性别 " width="55" header-align="center"
            align="center"></vxe-table-column>
          <vxe-table-column field="req_bedno" title="床号 " width="80" header-align="center"></vxe-table-column>
          <vxe-table-column field="emer_flag" title="加急 " width="50" header-align="center" align="center">
            <template v-slot="{ row }">
              <div style="color: red">{{ row.emer_flag == "0" ? "" : "急" }}</div>
            </template>
          </vxe-table-column>
          <vxe-table-column field="req_statname" title="申请状态 " width="82" header-align="center"></vxe-table-column>
          <vxe-table-column field="req_deptname" title="申请科室 " width="124" header-align="center"></vxe-table-column>
          <vxe-table-column field="req_wardname" title="病人病区 " width="125" header-align="center"></vxe-table-column>
          <vxe-table-column field="sample_items" title="项目描述 " width="203" header-align="center"></vxe-table-column>
          <vxe-table-column field="amount" title="金额 " width="75" header-align="center" align="right"></vxe-table-column>
          <vxe-table-column field="req_docname" title="申请医生 " width="85" header-align="center"></vxe-table-column>
          <vxe-table-column field="pat_typename" title="病人类型 " width="87" header-align="center"></vxe-table-column>
          <vxe-table-column field="create_dt" title="条码生成时间 " width="130" header-align="center"
            :formatter="['toDateString', 'yyyy-MM-dd HH:mm:ss']"></vxe-table-column>
          <vxe-table-column field="rcvmsg" title="消息 " width="105" header-align="center"></vxe-table-column>
          <template v-slot:fix>
            <div style="font-size: 12px; padding-top: 3px; background-color: #fff; text-align: center">
              <span style="margin-left: 35px; color: blue">已收: {{ rcvcnt }}</span><span
                style="color: red; margin-left: 35px">重复签收: {{ reRcvcnt }}</span>
            </div>
          </template>
        </vxe-table>
      </div>
    </div>
    <div class="pagebottom">
      <vxe-table show-overflow ref="xTable2" height="auto" :select-config="{ reserve: true }" :data.sync="tableData3"
        border size="mini" show-header-overflow highlight-current-row highlight-hover-row resizable stripe>
        <vxe-table-column field="barcode" title="条码号 " width="110" header-align="center"></vxe-table-column>
        <vxe-table-column field="req_groupna" title="分单类别 " width="86" header-align="center">
          <template v-slot="{ row }">
            <div v-bind:style="{ background: $pub.ColorChange(row.tube_color), color: $pub.colorConvert(row.tube_color) }"
              style="height: 20px; width: 120px">{{ !!row.req_groupna ? row.req_groupna : "" }}</div>
          </template>
        </vxe-table-column>
        <vxe-table-column field="pat_no" title="病人号 " width="105" header-align="center"></vxe-table-column>
        <vxe-table-column field="pat_name" title="姓名 " width="70" header-align="center"></vxe-table-column>
        <vxe-table-column field="pat_sexname" title="性别 " width="55" header-align="center"
          align="center"></vxe-table-column>
        <vxe-table-column field="req_bedno" title="床号 " width="80" header-align="center"></vxe-table-column>
        <vxe-table-column field="emer_flag" title="加急 " width="50" header-align="center" align="center">
          <template v-slot="{ row }">
            <div style="color: red">{{ row.emer_flag == "0" ? "" : "急" }}</div>
          </template>
        </vxe-table-column>
        <vxe-table-column field="req_statname" title="申请状态 " width="82" header-align="center"></vxe-table-column>
        <vxe-table-column field="req_deptname" title="申请科室 " width="124" header-align="center"></vxe-table-column>
        <vxe-table-column field="req_wardname" title="病人病区 " width="125" header-align="center"></vxe-table-column>
        <vxe-table-column field="sample_items" title="项目描述 " width="203" header-align="center"></vxe-table-column>
        <vxe-table-column field="amount" title="金额 " width="75" header-align="center" align="right"></vxe-table-column>
        <vxe-table-column field="req_docname" title="申请医生 " width="85" header-align="center"></vxe-table-column>
        <vxe-table-column field="pat_typename" title="病人类型 " width="87" header-align="center"></vxe-table-column>
        <vxe-table-column field="create_dt" title="条码生成时间 " width="130" header-align="center"
          :formatter="['toDateString', 'yyyy-MM-dd HH:mm:ss']"></vxe-table-column>
        <vxe-table-column field="rcvmsg" title="消息 " width="105" header-align="center"></vxe-table-column>
        <template v-slot:fix>
          <div style="font-size: 12px; padding-top: 3px; background-color: #fff; text-align: center">
            <span style="color: blue">共: {{ sumcnt }}</span><span style="margin-left: 35px; color: blue">已收: {{ hasRcvcnt
            }}</span><span style="color: red; margin-left: 35px">未收: {{ unrcvcnt }}</span>
          </div>
        </template>
      </vxe-table>
    </div>
    <el-dialog ref="edialog01" v-el-drag-dialog :append-to-body="true" :close-on-click-modal="false" title="签收提示"
      :visible.sync="diagPromiseVisible1">
      <div v-for="msg in msglist" :key="msg.title" style="over-flow: auto">
        <h3 style="color: red">{{ msg.Item1 }}</h3>
        <h5 v-html="msg.Item2"></h5>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="diagPromise_click(-1)" :autofocus="true">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog ref="edialog02" v-el-drag-dialog :append-to-body="true" :close-on-click-modal="false" title="签收提示"
      :visible.sync="diagPromiseVisible2">
      <div v-for="msg in msglist" :key="msg.title" style="over-flow: auto">
        <h3 style="color: red">{{ msg.Item1 }}</h3>
        <h5 v-html="msg.Item2"></h5>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="diagPromise_click(1)" :autofocus="true">是</el-button>
        <el-button @click="diagPromise_click(0)">否</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script type="text/javascript">
export default {
  props: { tableData2: { type: Array }, appendtobody: { type: Boolean, default: true }, remindtxt: { type: String }, userinfoval: { type: String }, name: { type: String }, packagebarcode: { type: String } },
  data() {
    return {
      alltableData: [],
      tableData1: [],
      tableData3: [],
      barcodetxt: "",
      // rcvcnt: 0,
      // unrcvcnt: 0,
      reRcvcnt: 0,
      // hasRcvcnt: 0,
      rcv_invalidmsgbox: "",
      rcv_dupnomsgbox: "",
      rcv_showinvalid: "",
      allowergrouprcvduplicate: "",
      diagPromiseVisible1: false,
      diagPromiseVisible2: false,
      msglist: [],
      translist: {},
      isclickconfirmbtn: "",// LIS211-是否点击送达确认按钮进行送达
      //packagebarcode: "",
      //transtype: ""
    };
  },
  created() {
    this.alltableData = this.tableData2;
    switch (this.name) {
      case "LIS221": //工作组签收
        this.InitPage221();
        break;
      case "LIS211":
        this.InitPage211();
        this.InitPage();
        break;
      default:
        this.InitPage();
        break;
    }
  },
  watch: {
    // trans(value) {
    //     console.log("spec", value);
    //     if (!!value) {
    //         this.translist = JSON.parse(value);
    //         this.transtype = this.translist.transtype;
    //         this.packagebarcode = this.translist.packagebarcode;
    //     } else {
    //         this.transtype = "";
    //         this.packagebarcode = "";
    //     }
    // }
  },
  computed: {
    rcvcnt() {
      //上方已签收
      let data = this.tableData1.filter((item) => Number(item.req_stat) >= 4);
      return data.length;
    },
    sumcnt() {
      //下方总共
      return this.tableData3.length;
    },
    unrcvcnt() {
      //下方未签收
      let data = this.tableData3.filter((item) => Number(item.req_stat) < 4);
      return data.length;
      // return this.tableData2.length - this.rcvcnt;
    },
    hasRcvcnt() {
      //下方已签收
      let data = this.tableData3.filter((item) => Number(item.req_stat) >= 4);
      return data.length;
    },
  },
  mounted() {
    this.barcodetxt = "";
  },
  updated() {

  },
  methods: {
    InitPage() {
      this.$tool
        .request({
          url: this.name == 'NBS311' ? "NBS/NBS3/NBS311" : "LIS/LIS2/LIS212",
          method: "post",
          data: {
            action: "getoptions",
          },
        })
        .then((res) => {
          this.rcv_invalidmsgbox = res.rcv_invalidmsgbox; //签收异常总是弹出提示框
          this.rcv_dupnomsgbox = res.rcv_dupnomsgbox; //重复签收不弹窗
          this.rcv_showinvalid = res.rcv_showinvalid; //列表中显示无效签收的数据

          this.alltableData = this.tableData2; // 接收页面传过来的数据
          for (let index = 0; index < this.alltableData.length; index++) {
            const element = this.alltableData[index];
            if (Number(element.req_stat) >= 4) {
              this.tableData1.splice(0, 0, element);
            }
            else {
              this.tableData3.splice(0, 0, element);
            }
          }
        })
        .catch((e) => {
          console.log("error", e);
        });
    },
    InitPage221() {
      this.$tool
        .request({
          url: "LIS/LIS2/LIS221",
          method: "post",
          data: {
            action: "getoptions",
          },
        })
        .then((res) => {
          this.rcv_invalidmsgbox = res.rcv_invalidmsgbox; //签收异常总是弹出提示框
          this.rcv_dupnomsgbox = res.rcv_dupnomsgbox; //重复签收不弹窗
          this.rcv_showinvalid = res.rcv_showinvalid; //列表中显示无效签收的数据
          this.allowergrouprcvduplicate = res.allowergrouprcvduplicate; //工作组重复签收时操作方式  0 不提醒|1 提醒|2 拒绝
          this.alltableData = this.tableData2; // 接收页面传过来的数据

          for (let index = 0; index < this.alltableData.length; index++) {
            const element = this.alltableData[index];
            if (Number(element.req_stat) >= 4) {
              this.tableData1.splice(0, 0, element);
            }
            else {
              this.tableData3.splice(0, 0, element);
            }
          }
        })
        .catch((e) => {
          console.log("error", e);
        });
    },
    InitPage211() {
      this.$tool
        .request({
          url: "LIS/LIS2/LIS211",
          method: "post",
          data: {
            action: "getoptions",
          },
        })
        .then((res) => {
          this.isclickconfirmbtn = res.isclickconfirmbtn; //是否点击送达确认按钮进行送达
        })
        .catch((e) => {
          console.log("error", e);
        });
    },
    btnClose() {
      this.barcodetxt = "";
      this.$emit("cancel", this.tableData1, "sus");
    },
    diagPromise_click(t) {
      if (t == 0) {
        //点了取消按钮
        this.$refs.edialog02.Cancel();
      } else if (t == 1) {
        //点了确定按钮
        this.$refs.edialog02.Confirm();
      } else {
        this.$refs.edialog01.Confirm();
      }
    },
    async dialogok(barcode, rcvmsg) {
      try {
        const res = await this.$tool.request({
          url: "LIS/LIS2/LIS212",
          method: "post",
          data: {
            action: "rcvbarcode",
            barcode: barcode,
            rcvmsg: rcvmsg,
            //senduser: this.userinfoval
          },
        });
        return res;
      } catch (e) {
        this.$message.warning(e);
      }
    }, //单个消息拼接
    async msginfo(msg, name) {
      let gmsg = "";
      msg.forEach((item) => {
        if ((gmsg = "")) {
          gmsg = name + "," + item.Item1;
        } else {
          gmsg = gmsg + "," + item.Item1;
        }
      });
      return gmsg;
    }, //消息列表拼接
    async msginfoList(msglist, name) {
      if (msglist.length > 0) {
        msglist.forEach((item) => {
          item.Item1 = name + item.Item1;
        });
      }
      return msglist;
    },
    async SingRcvBarcode(barcode) {
      var reg = new RegExp("[\\u4E00-\\u9FFF]+", "g");
      if (reg.test(barcode)) {
        //避免弹窗后按钮回车再次走入这个事件
        return false;
      }
      switch (this.name) {
        case "LIS211": //标本送达
          await this.SingRcvLIS211(barcode);
          break;
        case "LIS212": //标本签收
          await this.SingRcvLIS212(barcode);
          break;
        case "LIS221": //工作组标本签收
          await this.SingRcvLIS221(barcode);
          break;
        case "LIS222": //标本流转打包
          await this.SingRcvLIS222(barcode);
          break;
        case "NBS311": //输血科标本签收
          await this.SingRcvNBS311(barcode);
          break;
        default:
          break;
      }
      this.HightlightPos(this.barcodetxt);
    },
    async SingRcvLIS211(barcode) {
      try {
        let data = this.tableData2.filter((item) => item.barcode == barcode);
        if (data.length > 0) {
          if (this.isclickconfirmbtn == "1") {
            //点击送达按钮进行送达，直接返回确认信息
            this.tableData1.splice(0, 0, data[0]);
            this.$refs.xTable1.setCurrentRow(data[0]);
            await this.delHasScanRow(barcode).then((row) => {
              this.$refs.xTable2.setCurrentRow(row);
            });
            this.barcodetxt = barcode + " 成功！";
          }
          else {
            const res = await this.$tool.request({
              url: "LIS/LIS2/LIS211",
              method: "post",
              data: {
                action: "packagescan1by1",
                senduser: this.userinfoval,
                barcode: barcode,
                packageBarcode: this.packagebarcode,
                transtype: this.transtype,
              },
            });
            switch (res.flag) {
              case 1:
                if (data.length > 0) {
                  this.tableData1.splice(0, 0, data[0]);
                  this.$refs.xTable1.setCurrentRow(data[0]);
                  await this.delHasScanRow(barcode).then((row) => {
                    this.$refs.xTable2.setCurrentRow(row);
                  });
                }
                this.barcodetxt = barcode + " 成功！";
                break;
              default:
                this.$alert(res.msg);
                this.barcodetxt = barcode + " 失败！" + res.msg;
                break;
            }
          }
        } else {
          this.barcodetxt = barcode + " 不在该打包条码中！";
          this.HightlightPos(this.barcodetxt);
        }
      } catch (e) {
        this.$alert(e);
      }
    },
    async SingRcvLIS212(barcode) {
      try {
        let row = this.tableData2.filter((item) => item.barcode == barcode);
        if (row.length > 0) {
          const res = await this.$tool.request({
            url: "LIS/LIS2/LIS212",
            method: "post",
            data: {
              action: "scanbarcode",
              barcode: barcode,
              isPackRcv: "0",
              senduser: this.userinfoval,
              packagebarcode: this.packagebarcode,
              transtype: this.transtype,
            },
          });
          // console.log("singrcv", res.flag);
          res.master.rcvmsg = await res.rcvmsg;
          //res.master.tube_color = await res.tube_color;
          switch (res.flag) {
            case 1:
              //条码签收成功
              // console.log("1", res);
              await this.tableData1.splice(0, 0, res.master);
              this.$refs.xTable1.setCurrentRow(res.master);
              await this.delHasScanRow(barcode).then((row) => {
                this.$refs.xTable2.setCurrentRow(row);
              });
              this.barcodetxt = await res.rcvmsg;
              //this.hasRcvcnt++;
              this.HightlightPos(this.barcodetxt);
              break;
            case -1:
              //console.log("-1", res);
              //条码签收失败
              let msg1 = row[0].barcode + "(" + row[0].pat_name + ")签收失败,";
              this.barcodetxt = await this.msginfo(res.msgList, msg1); //扫描框显示签收消息
              this.msglist = await this.msginfoList(res.msgList, msg1); //弹窗显示签收消息
              if (this.barcodetxt.indexOf("重复签收") > -1) this.reRcvcnt++;
              await this.rcvmsgfault(-1, "", "");
              await this.rcv_showinvalidList(barcode, res.master); //列表中是否显示无效签收的数据
              this.HightlightPos(this.barcodetxt);
              break;
            case 0:
              //提示(有弹窗)->是与否->走入dialogok(已退回的标本如果勾选选项0)
              // console.log("0", res);
              let msg2 = row[0].barcode + "(" + row[0].pat_name + ")是否签收？此为";
              this.barcodetxt = await this.msginfo(res.msgList, msg2); //扫描框显示签收消息
              this.msglist = await this.msginfoList(res.msgList, msg2); //弹窗显示消息
              let retdata = await this.rcvmsgfault(0, row[0].barcode, res.rcvmsg); //可以签收会创建可签收消息
              // console.log("retdata", retdata);
              if (!retdata) {
                //退回的标本不签收，则为无效签收
                await this.rcv_showinvalidList(barcode, res.master); //列表中是否显示无效签收的数据
                break;
              }
              // 退回的标本签收，看签收状态
              switch (retdata.flag) {
                case 1:
                  //条码签收成功
                  // console.log("0-1", res);
                  retdata.master.rcvmsg = await retdata.msg; //表格显示签收消息
                  //  retdata.master.tube_color = await retdata.tube_color;
                  await this.tableData1.splice(0, 0, retdata.master);
                  this.$refs.xTable1.setCurrentRow(retdata.master);
                  await this.delHasScanRow(barcode).then((row) => {
                    this.$refs.xTable2.setCurrentRow(row);
                  });
                  this.barcodetxt = await retdata.msg;
                  // this.hasRcvcnt++;
                  this.HightlightPos(this.barcodetxt);
                  break;
                case -1:
                  //条码签收失败
                  //console.log("0--1", res);
                  let msg3 = row[0].barcode + "(" + row[0].pat_name + ")签收失败,";
                  this.msglist = await this.msginfoList(retdata.msgList, msg3); //弹窗显示消息
                  row[0].rcvmsg = await this.msginfo(retdata.msgList, msg3); //表格显示签收消息
                  await this.rcv_showinvalidList(barcode, res.master); //列表中是否显示无效签收的数据
                  await this.rcvmsgfault(-1, "", "");
                  break;
                case 0:
                  //提示 条码签收
                  console.log("res", res); //这种状态不存在，暂时写上
                  break;
              }
              this.rcvkey++;
              break;
            case -99:
              //代码出错->直接显示信息
              row[0].rcvmsg = await res.errmsg; //表格显示签收消息
              this.barcodetxt = await res.errmsg; //扫描框显示签收消息
              await this.rcv_showinvalidList(barcode, res.master); //列表中是否显示无效签收的数据
              this.rcvkey++;
              break;
          }
        } else {
          this.barcodetxt = barcode + " 不在打包条码中，无法签收！";
          this.HightlightPos(this.barcodetxt);
        }
      } catch (e) {
        this.$alert(e);
      }
    },
    async SingRcvLIS221(barcode) {
      try {
        let row = this.tableData2.filter((item) => item.barcode == barcode);
        let senduser = this.userinfoval.split(":")[0];
        let userno = this.userinfoval.split(":")[1];
        if (row.length > 0) {
          const res = await this.$tool.request({
            url: "LIS/LIS2/LIS221",
            method: "post",
            data: {
              action: "scanbarcode",
              barcode: barcode,
              isPackRcv: "0",
              senduser: senduser,
              userno: userno,
              labgroupid: "",
              packagebarcode: this.packagebarcode,
              transtype: this.transtype,
            },
          });

          res.master.rcvmsg = await res.rcvmsg;

          switch (res.flag) {
            case 1:
              //条码签收成功
              // console.log("1", res);
              await this.tableData1.splice(0, 0, res.master);
              this.$refs.xTable1.setCurrentRow(res.master);
              await this.delHasScanRow(barcode).then((row) => {
                this.$refs.xTable2.setCurrentRow(row);
              });
              this.barcodetxt = await res.rcvmsg;
              //this.hasRcvcnt++;
              this.HightlightPos(this.barcodetxt);
              break;
            case -1:
              //console.log("-1", res);
              //条码签收失败
              let msg1 = row[0].barcode + "(" + row[0].pat_name + ")签收失败,";
              this.barcodetxt = await this.msginfo(res.msgList, msg1); //扫描框显示签收消息
              this.msglist = await this.msginfoList(res.msgList, msg1); //弹窗显示签收消息
              if (this.barcodetxt.indexOf("重复签收") > -1) this.reRcvcnt++;
              await this.rcvmsgfault(-1, "", "");
              await this.rcv_showinvalidList(barcode, res.master); //列表中是否显示无效签收的数据
              this.HightlightPos(this.barcodetxt);
              break;
            case 0:
              //提示(有弹窗)->是与否->走入dialogok(已退回的标本如果勾选选项0)
              // console.log("0", res);
              let msg2 = row[0].barcode + "(" + row[0].pat_name + ")是否签收？此为";
              this.barcodetxt = await this.msginfo(res.msgList, msg2); //扫描框显示签收消息
              this.msglist = await this.msginfoList(res.msgList, msg2); //弹窗显示消息
              let retdata = await this.rcvmsgfault(0, row[0].barcode, res.rcvmsg); //可以签收会创建可签收消息
              // console.log("retdata", retdata);
              if (!retdata) {
                //退回的标本不签收，则为无效签收
                await this.rcv_showinvalidList(barcode, res.master); //列表中是否显示无效签收的数据
                break;
              }
              // 退回的标本签收，看签收状态
              switch (retdata.flag) {
                case 1:
                  //条码签收成功
                  // console.log("0-1", res);
                  retdata.master.rcvmsg = await retdata.msg; //表格显示签收消息
                  //  retdata.master.tube_color = await retdata.tube_color;
                  await this.tableData1.splice(0, 0, retdata.master);
                  this.$refs.xTable1.setCurrentRow(retdata.master);
                  await this.delHasScanRow(barcode).then((row) => {
                    this.$refs.xTable2.setCurrentRow(row);
                  });
                  this.barcodetxt = await retdata.msg;
                  // this.hasRcvcnt++;
                  this.HightlightPos(this.barcodetxt);
                  break;
                case -1:
                  //条码签收失败
                  //console.log("0--1", res);
                  let msg3 = row[0].barcode + "(" + row[0].pat_name + ")签收失败,";
                  this.msglist = await this.msginfoList(retdata.msgList, msg3); //弹窗显示消息
                  row[0].rcvmsg = await this.msginfo(retdata.msgList, msg3); //表格显示签收消息
                  await this.rcv_showinvalidList(barcode, res.master); //列表中是否显示无效签收的数据
                  await this.rcvmsgfault(-1, "", "");
                  break;
                case 0:
                  //提示 条码签收
                  console.log("res", res); //这种状态不存在，暂时写上
                  break;
              }
              this.rcvkey++;
              break;
            case -99:
              //代码出错->直接显示信息
              row[0].rcvmsg = await res.errmsg; //表格显示签收消息
              this.barcodetxt = await res.errmsg; //扫描框显示签收消息
              await this.rcv_showinvalidList(barcode, res.master); //列表中是否显示无效签收的数据
              this.rcvkey++;
              break;
            case 66:
              if (this.allowergrouprcvduplicate == "2") {
                that.barcodetxt = barcode + "  已签收，不能重复签收";
              } else if (this.allowergrouprcvduplicate == "1") {
                this.$confirm("该条码已被签收，是否重复签收?", "提示", {
                  confirmButtonText: "确定",
                  cancelButtonText: "取消",
                  type: "warning",
                })
                  .then(() => {
                    // 直接调用
                    this.$tool
                      .request({
                        url: "LIS/LIS2/LIS221",
                        method: "post",
                        data: {
                          action: "grouprcvbarcode",
                          barcode: barcode,
                          senduser: senduser,
                          userno: userno,
                          labgroupid: "",
                          isPackRcv: "0",
                        },
                      })
                      .then((res) => {
                        this.tableData1.splice(0, 0, res.master);
                        this.$refs.xTable1.setCurrentRow(res.master);
                        this.delHasScanRow(barcode).then((row) => {
                          this.$refs.xTable2.setCurrentRow(row);
                        });
                        this.barcodetxt = res.rcvmsg;
                        //this.hasRcvcnt++;
                        this.HightlightPos(this.barcodetxt);
                      })
                      .catch((res) => {
                        that.barcodetxt = res.rcvmsg; //列表显示签收消息
                      });
                  })
                  .catch(() => {
                    that.barcodetxt = barcode + "  已签收，不能重复签收";
                  });
              }

              break;
          }
        } else {
          this.barcodetxt = barcode + " 不在打包条码中，无法签收！";
          this.HightlightPos(this.barcodetxt);
        }
      } catch (e) {
        this.$alert(e);
      }
    },
    async SingRcvLIS222(barcode) {
      try {
        //之后的操作和原来的一样，调用packagescan1by1是要传入packagebarcode
        let data = this.tableData2.filter((item) => item.barcode == barcode);
        if (data.length > 0) {
          const res = await this.$tool.request({
            url: "LIS/LIS2/LIS222",
            method: "post",
            data: {
              action: "packagescan1by1",
              barcode: barcode,
              senduser: this.userinfoval,
              packagebarcode: this.packagebarcode,
            },
          });
          console.log("lis222", res);
          switch (res.flag) {
            case 1:
              res.master.lastbarcode = data[0].barcode;
              res.master.lastpatname = data[0].pat_name;
              this.tableData1.splice(0, 0, res.master);
              this.$refs.xTable1.setCurrentRow(res.master);
              await this.delHasScanRow(barcode).then((row) => {
                this.$refs.xTable2.setCurrentRow(row);
              });
              if (data.length > 0) {
                this.$emit("change", res.master, false);
              } else {
                this.$emit("change", res.master, true);
              }
              break;
            default:
              let msg1 = barcode;
              this.msglist = await this.msginfoList(res.msgList, msg1); //弹窗显示签收消息
              await this.rcvmsgfault(-1, "", "");
              break;
          }
        } else {
          this.barcodetxt = barcode + " 不在打包条码中！";
          this.HightlightPos(this.barcodetxt);
        }
      } catch (e) {
        this.$alert(e);
      }
    },
    async SingRcvNBS311(barcode) {
      try {
        let row = this.tableData2.filter((item) => item.barcode == barcode);
        if (row.length > 0) {
          const res = await this.$tool.request({
            url: "NBS/NBS3/NBS311",
            method: "post",
            data: {
              action: "ScanBarcode",
              barcode: barcode,
              senduser: this.userinfoval
            },
          });
          // console.log("singrcv", res.flag);
          res.master.rcvmsg = await res.rcvmsg;
          //res.master.tube_color = await res.tube_color;
          switch (res.flag) {
            case 1:
              //条码签收成功
              // console.log("1", res);
              await this.tableData1.splice(0, 0, res.master);
              this.$refs.xTable1.setCurrentRow(res.master);
              await this.delHasScanRow(barcode).then((row) => {
                this.$refs.xTable2.setCurrentRow(row);
              });
              this.barcodetxt = await res.rcvmsg;
              //this.hasRcvcnt++;
              this.HightlightPos(this.barcodetxt);
              break;
            case -1:
              //console.log("-1", res);
              //条码签收失败
              let msg1 = row[0].barcode + "(" + row[0].pat_name + ")签收失败,";
              this.barcodetxt = await this.msginfo(res.msgList, msg1); //扫描框显示签收消息
              this.msglist = await this.msginfoList(res.msgList, msg1); //弹窗显示签收消息
              if (this.barcodetxt.indexOf("重复签收") > -1) this.reRcvcnt++;
              await this.rcvmsgfault(-1, row[0].barcode, this.barcodetxt);
              await this.rcv_showinvalidList(barcode, res.master); //列表中是否显示无效签收的数据
              this.HightlightPos(this.barcodetxt);
              break;
            case 0:
              //提示(有弹窗)->是与否->走入dialogok(已退回的标本如果勾选选项0)
              // console.log("0", res);
              let msg2 = row[0].barcode + "(" + row[0].pat_name + ")是否签收？此为";
              this.barcodetxt = await this.msginfo(res.msgList, msg2); //扫描框显示签收消息
              this.msglist = await this.msginfoList(res.msgList, msg2); //弹窗显示消息
              let retdata = await this.rcvmsgfault(0, row[0].barcode, res.rcvmsg); //可以签收会创建可签收消息
              // console.log("retdata", retdata);
              if (!retdata) {
                //退回的标本不签收，则为无效签收
                await this.rcv_showinvalidList(barcode, res.master); //列表中是否显示无效签收的数据
                break;
              }
              // 退回的标本签收，看签收状态
              switch (retdata.flag) {
                case 1:
                  //条码签收成功
                  // console.log("0-1", res);
                  retdata.master.rcvmsg = await retdata.msg; //表格显示签收消息
                  //  retdata.master.tube_color = await retdata.tube_color;
                  await this.tableData1.splice(0, 0, retdata.master);
                  this.$refs.xTable1.setCurrentRow(retdata.master);
                  await this.delHasScanRow(barcode).then((row) => {
                    this.$refs.xTable2.setCurrentRow(row);
                  });
                  this.barcodetxt = await retdata.msg;
                  // this.hasRcvcnt++;
                  this.HightlightPos(this.barcodetxt);
                  break;
                case -1:
                  //条码签收失败
                  //console.log("0--1", res);
                  let msg3 = row[0].barcode + "(" + row[0].pat_name + ")签收失败,";
                  this.msglist = await this.msginfoList(retdata.msgList, msg3); //弹窗显示消息
                  row[0].rcvmsg = await this.msginfo(retdata.msgList, msg3); //表格显示签收消息
                  await this.rcv_showinvalidList(barcode, res.master); //列表中是否显示无效签收的数据
                  await this.rcvmsgfault(-1, "", "");
                  break;
                case 0:
                  //提示 条码签收
                  console.log("res", res); //这种状态不存在，暂时写上
                  break;
              }
              this.rcvkey++;
              break;
            case -99:
              //代码出错->直接显示信息
              row[0].rcvmsg = await res.errmsg; //表格显示签收消息
              this.barcodetxt = await res.errmsg; //扫描框显示签收消息
              await this.rcv_showinvalidList(barcode, res.master); //列表中是否显示无效签收的数据
              this.rcvkey++;
              break;
          }
        } else {
          this.barcodetxt = barcode + " 不在打包条码中，无法签收！";
          this.HightlightPos(this.barcodetxt);
        }
      } catch (e) {
        this.$alert(e);
      }
    },
    async rcv_showinvalidList(barcode, resmaster) {
      if (this.rcv_showinvalid == "1") {
        //列表中显示无效签收的数据
        let data = this.$refs.xTable1.tableData;
        let Fdata = data.filter((item) => item.barcode == barcode);
        if (Fdata.length == 0) {
          await this.tableData1.splice(0, 0, resmaster);
          this.$refs.xTable1.setCurrentRow(resmaster);
          await this.delHasScanRow(barcode).then((row) => {
            this.$refs.xTable2.setCurrentRow(row);
          });
        }
      }
    },
    rcvmsgfault(index, barcode, rcvmsg) {
      switch (index) {
        case -1: //失败(有弹窗)->点确定->关闭dialog
          return new Promise((s, f) => {
            let flag = true; //判断是不是重复签收的条码
            console.log(rcvmsg);
            if (this.rcv_dupnomsgbox == "1") {
              //判断是否是重复签收的
              if (rcvmsg.indexOf("重复签收的条码") > -1) {
                flag = false;
              }
            }

            if (flag) {
              //1.重复签收不弹窗
              //2.签收异常总是弹出提示框
              if (this.rcv_invalidmsgbox == "1") {
                this.$refs.edialog01.ShowDialog().then(() => {
                  s();
                  return;
                });
              }
            }
            else {
              s();
              return;
            }
          });
        case 0: //提示(有弹窗)->是与否->走入dialogok（退回的标本考虑是否显示无效签收的数据）
          return new Promise((s, f) => {
            this.$refs.edialog02
              .ShowDialog()
              .then(() => {
                let data = this.dialogok(barcode, rcvmsg);
                s(data);
                return;
              })
              .catch(() => {
                s();
                return;
              });
          });
      }
    },
    delHasScanRow(barcode) {
      return new Promise((s, f) => {
        var alld = this.tableData3; //this.$refs.xTable2.tableData;
        var delrow = alld.filter((item) => item.barcode == barcode);
        if (delrow.length > 0) {
          var rowindex = alld.indexOf(delrow[0]);
          //this.$refs.xTable2.remove(delrow[0]);
          this.tableData3.splice(rowindex, 1);
          let havedata = this.tableData3; //this.$refs.xTable2.tableData;
          let selectrow = [];
          if (havedata.length > 0) {
            if (rowindex >= havedata.length) rowindex = havedata.length - 1;
            selectrow = havedata[rowindex];
          }
          s(selectrow);
          return;
        } else {
          f(null);
          return;
        }
      });
    },
    async msginfo(msg, name) {
      let gmsg = "";
      msg.forEach((item) => {
        if (gmsg == "") {
          gmsg = name + item.Item1;
        } else {
          gmsg = gmsg + "," + item.Item1;
        }
      });
      return gmsg;
    },
    HightlightPos(myValue) {
      //alert(myValue);
      if (myValue == "") return false;
      var myField = document.getElementById("barcodeid");
      if (myField.selectionStart || myField.selectionStart === 0) {
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        this.$nextTick(() => {
          myField.selectionStart = 0;
          myField.selectionEnd = startPos + myValue.length;
        }); // 这句是重点, 圈起来
        myField.focus();
        myField.setSelectionRange(myField, myField.selectionStart, myField.selectionEnd);
      }
    },
  },
};
</Script>

<style scoped>
.pagetop {
  width: 100%;
  height: 35px;
  display: -webkit-box;
  -webkit-box-pack: left;
  -webkit-box-align: center;
  border: 1px solid #ccc;
  position: absolute;
  z-index: 99;
}

.pagecenter {
  position: absolute;
  top: 40px;
  width: 100%;
  left: 0;
  bottom: 5px;
  overflow: auto;
}

.middle-center {
  position: absolute;
  height: 230px;
  left: 0px;
  right: 0px;
  overflow: auto;
}

.pagebottom {
  position: absolute;
  top: 275px;
  width: 100%;
  left: 0;
  height: 250px;
  bottom: 0px;
  overflow: auto;
}

.table1style tr td {
  padding: 0px 2px 0px 2px;
}</style>